#pragma once

#include "CDependencyObject.h"
#include "XamlCommonServiceProviderContext.h"
#include "XamlReader.h"
#include "XamlSavedContext.h"
#include "ObjectWriterSettings.h"
#include "XamlOptimizedNodeList.h"
#include "ObjectWriter.h"

class CTemplateContent :
	public CDependencyObject
{

protected: 
	CTemplateContent(CCoreServices *);
protected: 
	virtual ~CTemplateContent();
public: 
	static HRESULT Create(CDependencyObject * * ppObject, CREATEPARAMETERS * pCreate);
	virtual XBOOL DoesAllowMultipleAssociation();
	virtual XBOOL ParticipatesInManagedTreeInternal();
	HRESULT RecordXaml(const xshared_ptr<XamlCommonServiceProviderContext> & spServiceProviderContext, const xshared_ptr<XamlOptimizedNodeList> & spNodeList, const xshared_ptr<XamlSavedContext> & spTemplateContext);
	HRESULT PlayXaml(xshared_ptr<XamlReader> & rspReader);
	HRESULT get_TemplateContext(xshared_ptr<XamlSavedContext> &);
	HRESULT Load(const xshared_ptr<ObjectWriterSettings> & spObjectWriterSettings, XBOOL bTryOptimizeContent, CDependencyObject * * ppDependencyObject);
	HRESULT GetSavedEventRoot(CDependencyObject * * ppEventRoot);
private: 
	HRESULT PreResolveResources(const xshared_ptr<XamlCommonServiceProviderContext> & spServiceProviderContext);
	static HRESULT ResolveAndSaveResource(CCoreServices * pCore, const xshared_ptr<XamlCommonServiceProviderContext> & spServiceProviderContext, const xshared_ptr<XamlReader> & spReader, CResourceDictionary * pResourceDictionary, XBOOL * pbResourceFromReadOnlyDictionary);
	HRESULT RemoveObjectReferencesFromStack();
	HRESULT SaveEventRootWeakReference(const xshared_ptr<XamlCommonServiceProviderContext> & spServiceProviderContext);
	HRESULT SaveAmbientProperties(const xshared_ptr<XamlCommonServiceProviderContext> & spServiceProviderContext);
	HRESULT LoadAndOptimize(const xshared_ptr<XamlReader> & spReader, const xshared_ptr<ObjectWriter> & spObjectWriter, const xshared_ptr<XamlSchemaContext> & spSchemaContext, xshared_ptr<XamlOptimizedNodeList> & rspOptimizedNodeList);
	HRESULT TryOptimizeValue(const xshared_ptr<XamlReader> & spReader, const xshared_ptr<ObjectWriter> & spObjectWriter, const xshared_ptr<XamlWriter> & spOptimizedWriter);
	HRESULT TryOptimizeTypeConvertedProperty(const xshared_ptr<XamlProperty> & spXamlProperty, const xshared_ptr<XamlReader> & spReader, const xshared_ptr<ObjectWriter> & spObjectWriter, const xshared_ptr<XamlWriter> & spOptimizedWriter);
	HRESULT WriteToMatchingEnd(const xshared_ptr<XamlReader> & spReader, const xshared_ptr<XamlWriter> & spWriter, const xshared_ptr<XamlWriter> & spSecondaryWriter);
	HRESULT ShouldOptimizeProperty(const xshared_ptr<XamlProperty> & spProperty, XBOOL * pbShouldOptimizeProperty);
public: 
	virtual XUINT32 GetTypeIndex() const;
private: 
	xshared_ptr<XamlOptimizedNodeList> m_spNodeList; 
	xshared_ptr<XamlSavedContext> m_spXamlSavedContext;
	xref_ptr<CWeakRef> m_spEventRoot;
	XBOOL m_bContentAlreadyOptimized:1;
	XBOOL m_bContentHasResourceReferences:1;


};


